package com.chao.demo.controller;

import com.chao.demo.bean.RegisterRequest;
import com.chao.demo.bean.User;
import com.chao.demo.common.ResponseResult;
import com.chao.demo.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/auth")
public class AuthController {
    
    @Autowired
    private AuthService authService;
    
    /**
     * 用户注册
     */
    @PostMapping("/register")
    public ResponseResult<?> register(@RequestBody RegisterRequest request) {
        return authService.register(request);
    }
    
    /**
     * 用户登录
     */
    @PostMapping("/login")
    public ResponseResult<String> login(@RequestBody User user) {
        // 登录时尝试将account字段作为账号或手机号使用
        return authService.login(user.getAccount(), user.getPassword());
    }
    
    /**
     * 用户退出
     */
    @PostMapping("/logout")
    public ResponseResult<?> logout(@RequestHeader("Authorization") String token) {
        return authService.logout(token);
    }
    
    /**
     * 获取用户信息
     */
    @GetMapping("/userInfo")
    public ResponseResult<User> getUserInfo(@RequestHeader("Authorization") String token) {
        return authService.getUserInfo(token);
    }
}